package com.cl.myInterface.impl;

import com.cl.annotation.ClController;
import com.cl.container.ClBean;
import com.cl.container.MyContainer;
import com.cl.myInterface.DisposeClass;
import com.cl.postprocessor.IBeanPostProcessor;
import com.cl.util.ScannerClass;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/*
* 过滤处理不同的bean
* */
public class FilterBeanToContainer implements DisposeClass {
    @Override
    public int order() {
        return 2;
    }

    @Override
    public void dispose(List<Class> clazz) {
        MyContainer.getInjectClassMap().forEach((beanName, beanClazz) -> {
            if (!beanClazz.isInterface()) {
                try {
                    //根据扫描到的bean的class,过滤出 bean后置处理器
                    Object o = beanClazz.getDeclaredConstructor().newInstance();
                    if (o instanceof IBeanPostProcessor) {
                        //先将bean收集进容器
                        ClBean clBean = ScannerClass.preCreateBean(beanName,beanClazz);
                        if (clBean != null) {
                            //收集进后置处理器集合中
                            MyContainer.putPostProcessor(clBean.getBeanName(), clBean);
                        }
                    }

                    //收集controller
                    ClController annotation = beanClazz.getAnnotation(ClController.class);
                    if (annotation != null && !annotation.value().equals("") && annotation.isController()){
                        ScannerClass.crateCoController(annotation.value(),beanClazz);
                    }
                } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
